package com.kmzx.one.project.net;

import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class UserResult implements Serializable {

    private static final long serialVersionUID = -3343836042909842955L;

    protected Integer code;

    protected String msg;

    protected Integer errCode;

    protected String errMsg;

    protected Object data;

    protected String userId;

    protected String sessionKey;

    protected String unionId;

    protected String avatar;

    protected String nickName;

    public Integer getCode() {
        return code;
    }

    public UserResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public UserResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Integer getErrCode() {
        return errCode;
    }

    public UserResult setErrCode(Integer errCode) {
        this.errCode = errCode;
        return this;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public UserResult setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public UserResult setData(Object data) {
        this.data = data;
        return this;
    }
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }


    public UserResult initSuccCodeAndMsg(Integer code, String msg) {
        UserResult baseResult = UserResult.Builder.baseResult;
        baseResult.errCode = code;
        baseResult.errMsg = msg;
        baseResult.code = code;
        baseResult.msg = msg;
        return baseResult;
    }


    public UserResult initErrCodeAndMsg(Integer code, String msg) {
        UserResult baseResult = UserResult.Builder.baseResult;
        baseResult.errCode = code;
        baseResult.errMsg = msg;
        baseResult.code = code;
        baseResult.msg = msg;
        return baseResult;
    }


    public UserResult initSuccData(Object data) {
        UserResult baseResult = UserResult.Builder.SUCC();
        if (data != null) {
            if (data instanceof List) {
                if (CollectionUtils.isEmpty((List) data)) {
                    baseResult.setCode(201);
                    baseResult.setErrCode(201);
                    baseResult.setMsg("数据为空");
                    baseResult.setErrCode(201);
                }
            } else if (data instanceof Map) {
                if (CollectionUtils.isEmpty((Map) data)) {
                    baseResult.setCode(201);
                    baseResult.setErrCode(201);
                    baseResult.setMsg("数据为空");
                }
            }
        } else {
            baseResult.setCode(201);
            baseResult.setErrCode(201);
        }
        baseResult.data = data;
        return baseResult;
    }

    public UserResult initErrorCode(Integer code) {
        UserResult baseResult = UserResult.Builder.baseResult;
        baseResult.errCode = code;
        return baseResult;
    }

    public UserResult initErrorMsg(String msg) {
        UserResult baseResult = UserResult.Builder.FAIL();
//        baseResult.errCode = 500;
        baseResult.errMsg = msg;
        return baseResult;
    }

    public String getAvatar() {
        return avatar;
    }

    public UserResult setAvatar(String avatar) {
        this.avatar = avatar;
        return this;
    }

    public String getNickName() {
        return nickName;
    }

    public UserResult setNickName(String nickName) {
        this.nickName = nickName;
        return this;
    }

    private UserResult() {
    }

    public static class Builder {
        private static UserResult baseResult = new UserResult();

        private Builder() {
        }

        public static UserResult SUCC() {
            baseResult.errCode = 200;
            baseResult.code = 200;
            baseResult.errMsg = "OK";
            baseResult.msg = "OK";
            baseResult.data = null;
            baseResult.userId = null;
            baseResult.sessionKey = null;
            baseResult.nickName = null;
            baseResult.avatar = null;
            return baseResult;
        }

        public static UserResult FAIL() {
            baseResult.errCode = 400;
            baseResult.errMsg = "server is busying";
            baseResult.data = null;
            baseResult.userId = null;
            baseResult.sessionKey = null;
            return baseResult;
        }

        public static UserResult FAILServer() {
            baseResult.errCode = 500;
            baseResult.errMsg = "server is error";
            baseResult.data = null;
            baseResult.userId = null;
            baseResult.sessionKey = null;
            return baseResult;
        }
    }

    public String getUnionId() {
        return unionId;
    }

    public UserResult setUnionId(String unionId) {
        this.unionId = unionId;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("UserResult{");
        sb.append("code=").append(code);
        sb.append(", msg='").append(msg).append('\'');
        sb.append(", errCode=").append(errCode);
        sb.append(", errMsg='").append(errMsg).append('\'');
        sb.append(", data=").append(data);
        sb.append(", userId='").append(userId).append('\'');
        sb.append(", sessionKey='").append(sessionKey).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
